﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NexTrackDesktop.NexTrack;
using System.Windows.Media;
using System.Windows;
using NexTrackDesktop.ViewModels;

namespace NexTrackDesktop.Media
{
    /// <summary>
    /// This class plays music. It uses the built in System Media Player object
    /// </summary>
    public class NexTrackPlayer : INPCBase
    {
        public event Action MediaEnded;

        private MediaPlayer MediaPlayer;

        /// <summary>
        /// This method links the NexTrackPlayer to the System Media Player
        /// </summary>
        public NexTrackPlayer()
        {
            MediaPlayer = new MediaPlayer(); 
            MediaPlayer.MediaEnded += new EventHandler(MediaPlayer_MediaEnded);
        }

        /// <summary>
        /// This method stops music play and advances to next song.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MediaPlayer_MediaEnded(object sender, EventArgs e)
        {
            Playing = false;

            if (MediaEnded != null)
                MediaEnded();
        }

        // Now Playing Song property and accessor/modifier
        private Song _nowPlaying;
   
        public virtual Song NowPlaying
        {
            get { return _nowPlaying; }
            set { PlaySong(value); NotifyPropertyChanged(() => NowPlaying); }
        }

        // Playing boolean property and accessor/modifier
        private bool _playing;

        public virtual bool Playing
        {
            get { return _playing; }
            set { _playing = value; NotifyPropertyChanged(() => Playing); }
        }

        public int VolumeWidth = 100;

        // Get/Sets volume of mediaplayer
        public virtual double Volume
        {
            get { return MediaPlayer.Volume * VolumeWidth; }
            set { MediaPlayer.Volume = value / VolumeWidth; }
        }

        /// <summary>
        /// This method is used to get or set the position in the now playing song
        /// </summary>
        public double PositionValue
        {
            get { return MediaPlayer.Position.TotalSeconds; }
            set { MediaPlayer.Position = TimeSpan.FromSeconds(value); }
        }

        public double Duration
        {
            get { return MediaPlayer.NaturalDuration.HasTimeSpan ? MediaPlayer.NaturalDuration.TimeSpan.TotalSeconds : 1.0; }
        }

       /// <summary>
       /// This method plays the specified song.
       /// </summary>
       /// <param name="song">Song to play</param>
        public virtual void PlaySong(Song song)
        {
            _nowPlaying = song;
            MediaPlayer.Stop();
            MediaPlayer.Open(song.Location);
            Resume();
        }

        /// <summary>
        /// This method pauses the playing of the now playing song.
        /// </summary>
        public virtual void Pause()
        {
            Playing = false;
            MediaPlayer.Pause();
        }

        /// <summary>
        /// This method resumes playing of the now playing song.
        /// </summary>
        public virtual void Resume()
        {
            Playing = true;
            MediaPlayer.Play();
        }

        
    }
}
